/**
 * 
 */
package cn.biplam.front.template.control.pop;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.Html;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.html.base.Const.Position_Mode;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.template.Template;
import cn.biplam.front.template.control.pop.PopLevel.PopMenu_Layer;

/**
 * 弹出菜单
 */
public class PopMenu implements Template{
	
	private FramePopMenu frame;
	private String ID;
	private UserInfo mUser;
	private HtmlWindow prompt;
	private PopMenu_Layer promptLayer;

	private String sHomeUrl;
	
	/**
	 * 构造方法
	 * @param ID		Confirm窗口绑定对象ID(外部Div)
	 * @param sHomeUrl	主页Url
	 * @param sTitle	标题
	 * @throws ErrorMessage 
	 */
	public PopMenu(String ID,String sHomeUrl,UserInfo mUser,PopMenu_Layer layer) throws ErrorMessage{
		this.ID=ID;
		this.sHomeUrl=sHomeUrl;
		this.mUser=mUser;
		this.promptLayer = layer;
		this._init();
	}
	
	/**
	 * 
	 * @throws ErrorMessage
	 */
	private void _init() throws ErrorMessage{
		frame=new FramePopMenu(ID+"f",sHomeUrl,mUser);
	}
	
	/**
	 * 绑定Frame的确认按钮事件
	 * @param sScript	事件执行的js脚本
	 * @throws ErrorMessage
	 */
	public void bindSubmit(String sScript) throws ErrorMessage{
		this.frame.bindSubmit(sScript);
	}
	
	/**
	 * 返回设置好的HtmlWindow对象
	 * @return
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get() throws ErrorMessage{
		prompt=new HtmlDiv(ID,"pp");
		prompt.setZIndex(this.promptLayer.value);
		
		//创建背景层divnew HtmlDiv(this.ID+"b")
		HtmlDiv bg = new HtmlDiv(this.ID+"b");
		bg.setStyleHeight("100",Length_Unit.percent);
		bg.setStyleWidth("100",Length_Unit.percent);
		bg.setPositionMode(Position_Mode.Absolute);
		bg.setTop("0");
		bg.setLeft("0");
		bg.setZIndex(PopMenu_Layer.Back.toString());
		bg.setBackGroundImage(this.sHomeUrl+"/images/empty.gif");

		prompt.put(bg);
		
		
		PopMenu_Layer fLayer=  null;
		try{
			String sufic = this.promptLayer.name();
			fLayer = PopMenu_Layer.valueOf(sufic);
		}catch(Exception e){
			e.printStackTrace();
			throw new ErrorMessage(e,"弹出窗口层次错误");
		}
		frame.setLayerIndex(fLayer);
		
		/**装入设置好的Frame*/
		prompt.put(frame.get());
		/**绑定关闭脚本*/
		this.bindBodyClickClose(prompt);
		return this.prompt;
	}
	
	/**
	 * 向Frame正文区域放入Html对象
	 * @param html
	 * @throws ErrorMessage
	 */
	public void put(Html html) throws ErrorMessage{
		this.frame.put(html);
	}
	
	/**
	 * 向Frame容器放入Html对象
	 * @param html
	 * @throws ErrorMessage
	 */
	public void putFrame(Html html) throws ErrorMessage{
		this.frame.putFrame(html);
	}

	/**
	 * 向Popup容器内放入Html对象
	 * @param html
	 * @throws ErrorMessage
	 */
	public void putPopup(Html html) throws ErrorMessage{
		this.prompt.put(html);
	}
	
	private void bindBodyClickClose(HtmlWindow prompt) throws ErrorMessage{
		prompt.bind(Window_Event.OnClick, "fc('"+this.ID+"');");
	}
}
